﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;

public partial class MasterPage : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["USERNAME"] != null&&Session["IDENTITY"]=="manager")
        {
            lk1.Text = "注销";
           //获取certification为0的employee
            SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc.Open();
            SqlCommand get_count=new SqlCommand("select count(EMPLOYEE_NAME) from INFO_EMPLOYEE where ENTER_ID=@eid1 and CERTIFICATION=0",sc);
            get_count.Parameters.AddWithValue("@eid1",Session["ENTERID"].ToString());
            int uncertifiedcount=Convert.ToInt32(get_count.ExecuteScalar().ToString());
            sc.Close();
            lk2.Text = "您有" + uncertifiedcount + "条销售人员申请尚未处理";
            musername123.Text = "欢迎你，" + Session["USERNAME"]+"<br />您的身份是：管理员";
            nav.Visible = true;
            lk1.Visible = true;

        }
        else if (Session["USERNAME"] != null && Session["IDENTITY"] == "employee")
        {
            lk1.Text = "注销";
            SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc.Open();
            SqlCommand get_cert = new SqlCommand("select CERTIFICATION from INFO_EMPLOYEE where EMPLOYEE_NAME=@namee",sc);
            get_cert.Parameters.AddWithValue("@namee",Session["EMPLOYEENAME"]);
            while (Application["sqllock"] == "lock")
            {//检测是否已经锁住
                System.Threading.Thread.Sleep(2000);
            }
            Application["sqllock"] = "lock";//上锁 
            int a = Convert.ToInt32(get_cert.ExecuteScalar().ToString());
            Application["sqllock"] = "unlock";//解锁 
            sc.Close();
            if (a == 1)
                lk2.Text = "";
            else
                lk2.Text = "";
            musername123.Text = "欢迎你，" +Session["EMPLOYEENAME"]+"<br />隶属于："+Session["USERNAME"] + "<br /> 您的身份是：销售人员";
            lk1.Visible = true;
        }
        else
        {
            lk1.Text = "登录";
            musername123.Text = "";
            nav.Visible = false;
            lk1.Visible = false;
        }
    }
    protected void lk1_click(object sender, EventArgs e)
    {
        if (Session["USERNAME"] != null)
        {
            SqlConnection sc = new SqlConnection(ConfigurationManager.ConnectionStrings["wbkpt"].ConnectionString.ToString());
            sc.Open();
            if (Session["IDENTITY"].Equals("manager") == true)
            {
                SqlCommand down1 = new SqlCommand("update INFO_ENTER set STATUS=0 where ENTER_NAME=@name2", sc);
                down1.Parameters.AddWithValue("@name2", Session["USERNAME"]);
                while (Application["sqllock"] == "lock")
                {//检测是否已经锁住
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁 
                try
                {
                    down1.ExecuteNonQuery();
                }
                finally
                {
                    Application["sqllock"] = "unlock";//解锁 
                }
            }
            else if (Session["IDENTITY"].Equals("employee") == true)
            {
                SqlCommand down2 = new SqlCommand("update INFO_EMPLOYEE set STATUS=0 where EMPLOYEE_NAME=@name3", sc);
                down2.Parameters.AddWithValue("@name3", Session["EMPLOYEENAME"]);
                while (Application["sqllock"] == "lock")
                {//检测是否已经锁住
                    System.Threading.Thread.Sleep(2000);
                }
                Application["sqllock"] = "lock";//上锁 
                try
                {
                    down2.ExecuteNonQuery();
                }
                finally
                {
                    Application["sqllock"] = "unlock";//解锁
                }
            }
            sc.Close();
        }
        Session["USERNAME"] = null;
        Session["EMPLOYEENAME"] = null;
        Session["IDENTITY"] = null;
        Session["ENTERID"] = null;
        Response.Redirect("login.aspx");
    }

    protected void lk2_Click(object sender, EventArgs e)
    {
        if (Session["USERNAME"] != null && Session["IDENTITY"] == "manager")
        {//跳转到申请处理页面
            Response.Redirect("certification_employee.aspx");
        }
    }
}
